package club.banyuan.mbm;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class ProviderService {
    private int idCount = 1;
    private List<Provider> providerList = new ArrayList<>();

    // 增加
    public void addProvider(Provider provider) {
//        String regex = "1[3456789][0-9]{9}";
//        if (!provider.getPhone().matches(regex)){
//            throw new BadRequestException("电话号码输入错误！");
//        }
        provider.setId(idCount++);
        providerList.add(provider);
    }

    // 根据ID删除
    public void deleteProvider(int id) {
        providerList.removeIf(provider -> provider.getId() == id);
    }

    // 根据ID查询
    public Provider getProvider(int id) {
        for (Provider provider : providerList) {
            if (provider.getId() == id) {
                return provider;
            }
        }
        return null;
    }

    // 更改
    public void updateProvider(Provider provider) {
//        String regex = "1[3456789][0-9]{9}";
//        if (!provider.getPhone().matches(regex)){
//            throw new BadRequestException("电话号码输入错误！");
//        }
        Provider old = getProvider(provider.getId());
        if (old != null) {
            old.setName(provider.getName());
            old.setDesc(provider.getDesc());
            old.setPhone(provider.getPhone());
            old.setContactPerson(provider.getContactPerson());
        }
    }


    // 序列化保存在硬盘中
    public void save() {
        String path = getPath();
        checkFileExist(path);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
            writer.write(JSONObject.toJSONString(this));
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 反序列化
    public static ProviderService load() {
        String path = getPath();
        File file = new File(path);
        if (!file.exists()){
            File newFile = new File(file.getParent());
            newFile.mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new ProviderService();
        }else {
            try (InputStream inputStream = new FileInputStream(path)) {

                return JSONObject
                        .parseObject(inputStream, ProviderService.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    // 获取配置文件里的路径
    private static String getPath() {
        Properties properties = new Properties();
        try {
            properties.load(UserService.class.getResourceAsStream("/app.properties"));
        } catch (IOException e) {
            System.out.println("读取配置文件失败");
            System.exit(-1);
        }
        return properties.getProperty("provider.store.path");
    }

    // 判断文件是否存在
    private static void checkFileExist(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            File newFile = new File(file.getParent());
            newFile.mkdirs();
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public int getIdCount() {
        return idCount;
    }

    public void setIdCount(int idCount) {
        this.idCount = idCount;
    }

    public List<Provider> getProviderList() {
        return providerList;
    }

    public List<Provider> getProviderList(Provider provider) {
        List<Provider> rlt = new ArrayList<>();
        if (provider.getName() == null && provider.getDesc() != null) {
            for (Provider one : providerList) {
                if (one.getDesc().contains(provider.getDesc())){
                    rlt.add(one);
                }
            }
        }else if (provider.getName()!=null && provider.getDesc()==null){
            for (Provider one : providerList) {
                if (one.getName().contains(provider.getName())){
                    rlt.add(one);
                }
            }
        }else if (provider.getName() == null && provider.getDesc() == null){
            rlt = providerList;
        }else {
            for (Provider one : providerList) {
                if (one.getDesc().contains(provider.getDesc()) && one.getName().contains(provider.getName())){
                    rlt.add(one);
                }
            }
        }
        return rlt;

//        return providerList.stream().filter(t -> {
//            if (provider.getDesc().trim().length() == 0) {
//                return true;
//            } else {
//                return t.getDesc().contains(provider.getDesc());
//            }
//        }).filter(t -> {
//            if (provider.getName().trim().length() == 0) {
//                return true;
//            } else {
//                return t.getName().contains(provider.getName());
//            }
//        }).collect(Collectors.toList());
    }

    public void setProviderList(List<Provider> providerList) {
        this.providerList = providerList;
    }
}
